
require 'Map Generator/MapConstants'

class Map
	
	@islandStyle
	@height
	@width

	def initialize(cols, rows)

		@mapdata = Array.new(cols * rows)
		
		@width = cols
		@height = rows
		
		for x in (0..cols-1)
		
			for y in (0..rows-1)
			
				@mapdata[x + (y * @width)] = TILE_COASTLINE
			
			end #y
		
		end #x

	end #initialize
	
	def height
		@height
	end
	
	def width
		@width
	end
	
	def cell(x, y)
		
		#(-) index in ruby just counts from end of array back in
		if x < 0 or y < 0 
			return TILE_INVALID
		end
		
		if x > width-1 or y > height-1
			return TILE_INVALID
		end
			
		
		if @mapdata[x + (y * @width)] == nil
			return TILE_INVALID
		end
		
		return @mapdata[x + (y * @width)]
		
	end
	
	def setCell(x, y, value)
	
		@mapdata[x + (y * @width)] = value
	
	end
	
	def to_s

		stringResult = ""

		for row in (0..@height-1)
			stringResult += @mapdata[(row * @width), @width].join(".") + ".\n"
		end

		return stringResult 
	
	end
	
	def countryCount
	
		maxCountry = 0
	
		for x in (0..width)
			for y in (0..height)
		
				if cell(x, y) < WATERBODY_START and cell(x, y) > maxCountry
					maxCountry = cell(x, y)
				end
						
			end
		end
		
		return maxCountry
	
	end
	
end
